import unittest
from YUN.common.HTMLTestRunner import HTMLTestRunner
import os
# 执行case生成报告
# 获取根目录方法
proDir = os.path.abspath(os.path.dirname(__file__)) #根目录地址
#proDir=C:\Users\Administrator\Desktop\Uiframetest
# 拼接路径a+b
path = os.path.join(proDir, "report")
#path=C:\Users\Administrator\Desktop\Uiframetest\report
case_path = "\\test_case\\"
print(case_path)

class run():
    # 2、加载我们用例生成报告
    def runtest(self):
        #\\ 表示表示对\转义 \n \t制表符代表空格
        case_path=".\\test_case\\"
        #case_path=C:\Users\Administrator\Desktop\Uiframetest\test_case
        test_case = self.Creatsuite(case_path)#所有test自动化用例
        rep = os.path.join(path, "index.html")
        #rep=C:\Users\Administrator\Desktop\Uiframetest\report\index.html
        fp = open(rep, 'wb')
        #定义测试报告
        runner = HTMLTestRunner(stream=fp,title='云商WebUI测试报告',description='云商一轮WebUI自动化测试报告')

        #运行测试用例
        runner.run(test_case)
        fp.close()  #关闭报告文件

    #1、这里通过discover加载用例
    def Creatsuite(self,case_path):
        #unittest.TestLoader().discover()
        suites = unittest.defaultTestLoader.discover(
            start_dir=case_path,
            #C:\Users\Administrator\Desktop\Uiframetest\test_case
            pattern='test_*.py',
            top_level_dir=None
        )
        return suites

if __name__ == "__main__":
    run().runtest()

